<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>生成订单</title>
    <link href="../css.min/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="../css/header.css" />
    <link rel="stylesheet" href="../css/common.css" />
    <style>
    	.order-price{
    		font-weight: bold;
    	}
    	.bold{
    		font-weight: bold;
    	}
    </style>
</head>
<body>
	<header class="mui-bar mui-bar-nav">
	    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
	    <h1 class="mui-title">提交订单</h1>
	</header>
	<div class="mui-content">
		<div class="mui-card">
		    <ul class="mui-table-view">
		        <li class="mui-table-view-cell">
		            <a id="select-address" style="height: 65px;line-height: 65px;" class="mui-navigate-right">
		          		<h5 class="mui-ellipsis" style="height: 35px;line-height: 35px;">请选择收货地址</h5>	      
		            </a>
		        </li>
		        <li class="mui-table-view-cell mui-disabled">
		            <div class="mui-table">
		            	<div id="photo-des" class="mui-table-cell" style="width: 80%;">
		            		尺寸：**，相纸：**
		            	</div>
		            	<div id="photo-price" style="width: 20%;" class="mui-table-cell order-price mui-text-right">
		            		--
		            	</div>
		            </div>
		        </li>
		        <li class="mui-table-view-cell mui-disabled">
		            <div class="mui-table">
		            	<div class="mui-table-cell">
		            		运送方式
		            	</div>
		            	<div id="delivery-price" class="mui-table-cell order-price mui-text-right">
		            		--
		            	</div>
		            </div>
		        </li>
		        <li class="mui-table-view-cell mui-disabled">
		            <div class="mui-table">
		            	<div class="mui-table-cell">
		            		冲印份数
		            	</div>
		            	<div class="mui-table-cell mui-text-right">
		            		<div class="mui-numbox" data-numbox-min='1'>
								<button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
								<input id="printCountIpt" class="mui-input-numbox" type="number" />
								<button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
							</div>
		            	</div>
		            </div>
		        </li>
		        <li class="mui-table-view-cell mui-disabled">
		            <div class="mui-table">
		            	<div class="mui-table-cell">
		            		合计
		            	</div>
		            	<div id="total-price" class="mui-table-cell order-price mui-text-right">
		            		--
		            	</div>
		            </div>
		        </li>
		    </ul>
		</div>
		
		<div class="mui-input-row" style="margin: 10px 15px;">
			<textarea id="leaveMsg" rows="4" placeholder="~有什么跟卖家说的（可选）~"></textarea>
		</div>
		
		<div class="mui-content-padded">
			<button id="submit-btn" type="button" style="background-color: #EE4B62;border:1px solid #EE4B62;color: white;" class="mui-btn mui-btn-block">马上下单</button>
		</div>
		
	</div>
</body>
<script src="../js.min/mui.min.js"></script>
<script type="text/javascript" src="../js/config.js" ></script>
<script type="text/javascript" src="../js/utils.js" ></script>
<script type="text/javascript" src="../js/price.js" ></script>
<script type="text/javascript" src="../js/cache.js" ></script>
<script type="text/javascript" charset="utf-8">
    mui.init();
    var M=this;
    
    var printCountIpt=null,photo_priceDiv=null,delivery_priceDiv=null,
    total_priceDiv=null,photo_desDiv=null,select_addresslink=null,submitBtn=null,
    leaveMsg=null;
    document.addEventListener("DOMContentLoaded",function(){
    	photo_desDiv=document.getElementById("photo-des");
    	photo_priceDiv=document.getElementById("photo-price");
    	delivery_priceDiv=document.getElementById("delivery-price");
    	total_priceDiv=document.getElementById("total-price");
    	printCountIpt=document.getElementById("printCountIpt");
    	select_addresslink=document.getElementById("select-address");
    	submitBtn=document.getElementById("submit-btn");
    	leaveMsgTxt=document.getElementById("leaveMsg");
    	//数量
    	printCountIpt.addEventListener('change',function(){
    		var e=this;
    		
    		e.v=M.PrintCount=parseInt(printCountIpt.value);
    		M.TOTAL_PRICE=e.total=(e.v * parseFloat(M.PRICE_PHOTO)+parseInt(PRICE.delivery)).toFixed(2);
    		
    		total_priceDiv.innerHTML="¥"+e.total;
    		
    	});
    	
    	//选择收货地址
    	select_addresslink.addEventListener('tap',function(){
    		utils.openWindow("add_address","add_address.html",
    		"slide-in-right",300,false);
    	});
    	
    	//提交订单
    	submitBtn.addEventListener('tap',function(){
    		if(CACHE.Login()=="true" && CACHE.UserName()){
    			var e=this;
    			if(!M.contact_name || !M.contact_phone || !M.contact_address) return mui.alert("请选择收货人及地址");
    			if(!M.PhotoList) return mui.alert("信息填写不完整");
    			
    			e.wa=plus.nativeUI.showWaiting("正在下单中...");
    			e.roundID=new Date().getTime()+parseInt(Math.random()*100000000);
    			mui.ajax_query("submitOrderMenu!PPH",
    			{order_ID:e.roundID,product_des:photo_desDiv.innerText,product_price:photo_priceDiv.innerText,product_delivery:delivery_priceDiv.innerText,
    			product_count:M.PrintCount,product_total:total_priceDiv.innerText,leaveMsg:leaveMsgTxt.value||"",
    			contact_name:M.contact_name,contact_phone:M.contact_phone,contact_address:M.contact_address,
    			user_name:CACHE.UserName(),pay:2,photo_name:M.PhotoList},
    			function(data){
    				e.wa.close();
    				if(!data || data["code"] !=1) return mui.alert("下单出错了");
    				
    				mui.confirm("下单成功了！快到订单中心查看并支付吧","恭喜您",
    				["立刻支付","稍后支付"],
    				function(e){
    					if(e.index==0){
    						//跳转订单中心
    						//可能刷新订单中心webview
			    			e.orderView=plus.webview.getWebviewById("pages/order.html");
			    			if(e.orderView) mui.fire(e.orderView,"RefreshPage");
			    			
			    			setTimeout(function(){
			    				//关闭打印设置页面
				    			e.parentView=plus.webview.currentWebview().opener();
				    			e.parentView.close();
				    			//关闭本页面
				    			plus.webview.currentWebview().close();
				    			e.mainView=plus.webview.getLaunchWebview();
				    			if(e.mainView) mui.fire(e.mainView,"goOrder");
			    			},500);
			    			
    					}
    				});
    				
    			},
    			function(c,m){
    				e.wa.close();
    				mui.alert("下单失败");
    			});
    			
    		}else{
    			utils.openWindow("login","login.html","slide-in-right",300,false);
    		}
    	});
    	
    },false);
    
    /**
     * 有登录需求的需实现该监听
     */
    window.addEventListener("RefreshPage",function(e){
    	
    });
    
    window.addEventListener("UpdateAddress",function(e){
    	M.contact_name=e.detail.name;
    	M.contact_phone=e.detail.phone;
    	M.contact_address=e.detail.address;
    	
    	var h5=select_addresslink.querySelector('h5');
    	h5.classList.add("bold");
    	h5.innerHTML="收货人："+M.contact_name;
    	
    });
    
    /**
     * h5+ ready
     */
    mui.plusReady(function(){
    	var E=this;
    	var currentView=plus.webview.currentWebview(); 
    	E.SIZE=currentView.SIZE;
    	E.PAPER=currentView.PAPER;
    	M.PhotoList=currentView.PNStr;//要提交的照片名称
    	E.PHOTO_COUNT=currentView.PHOTO_COUNT;//已选照片数量
    	
    	//相片描述
    	photo_desDiv.innerHTML="尺寸："+E.SIZE+"，相纸："+E.PAPER;
    	
    	//单价
    	E.SIZE_ID=getIDbyDes(E.SIZE);
    	E.pric=getPriceBydes(E.SIZE_ID);
    	
    	if(E.pric !=null){
    		photo_priceDiv.innerHTML="¥ "+E.pric+" × "+E.PHOTO_COUNT;
   			M.PRICE_PHOTO=E.pric*E.PHOTO_COUNT;
    	}
    	
    	//运费
    	if(PRICE.delivery !=null){
    		delivery_priceDiv.innerHTML="快递  ¥"+PRICE.delivery;
    	}
    	
    	//合计
    	E.count=parseInt(printCountIpt.value);
    	M.TOTAL_PRICE=E.total=(E.count * parseFloat(M.PRICE_PHOTO)+parseInt(PRICE.delivery)).toFixed(2);
    		
    	total_priceDiv.innerHTML="¥"+E.total;
    	
    });
    
    /**
     * get size id
     * @param {Object} s
     */
    function getIDbyDes(s){
		s=s.substr(0,1);
		if(s=="3") return "three_inch";
		if(s=="4") return "forth_inch";
		if(s=="5") return "five_inch";
		if(s=="6") return "six_inch";
		if(s=="7") return "seven_inch";
		
		return null;
	}
    
    /**
     * get other by id
     * @param {Object} id
     */
    function getPriceBydes(id){
    	if(!PRICE.sizeArray) return null;
    	var arr=PRICE.sizeArray;
    	for (var i = 0; i < arr.length; i++) {
    		var v=arr[i];
    		if(id==v["id"]){
    			
    			return v["price"];
    		}
    	}
    	
    	return null;
    }
    
</script>
</html>